﻿Imports MyDLL

Public Class Form9

    Dim Nav As New MyDLL.RecordNav

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim C As String = TextBox7.Text
        Dim Owner As String = TextBox1.Text

        If CheckBox1.Checked Then
            Dim Ownerxml As XElement = _
            <Owner><%= Owner %></Owner>

            Form1.RichTextBox1.Text = Form1.RichTextBox1.Text & "<Account>" & vbCr _
                                      & "<Type>Accounting</Type>" & vbCr & Ownerxml.ToString & vbCr
        End If

        If CheckBox2.Checked Then
            Dim Ownerxaml As XElement = _
            <Paragraph FontFamily="Arial" FontSize="20pt">
                Account History: Transactions for <%= Owner %>
            </Paragraph>

            Form1.RichTextBox2.Text = Form1.RichTextBox2.Text & _
            "<FlowDocument xmlns=" & C & "http://schemas.microsoft.com/winfx/2006/xaml/presentation" & C & ">" & vbCr & Ownerxaml.ToString & vbCr
        End If
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim Count As String = TextBox6.Text

        If CheckBox1.Checked Then
            Dim Counter As XElement = _
            <Counter>
                <Count><%= Count %></Count>
            </Counter>

            Form1.RichTextBox1.Text = Form1.RichTextBox1.Text & Counter.ToString & vbCr & "</Account>"
        End If

        If CheckBox2.Checked Then
            Form1.RichTextBox2.Text = Form1.RichTextBox2.Text & "</FlowDocument>"
        End If
    End Sub
    Private Sub Add()
        Dim Euros As Double = TextBox9.Text
        Dim Add As Double = TextBox4.Text
        Dim Ans As Double

        Ans = Euros + Add
        TextBox9.Text = Ans
    End Sub

    Private Sub Remove()
        Dim Euros As Double = TextBox9.Text
        Dim Remove As Double = TextBox5.Text
        Dim Ans As Double

        Ans = Euros - Remove
        TextBox9.Text = Ans
    End Sub
    Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
        Try
            Add()
            Remove()
        Catch ex As Exception
            MsgBox("Please insert the needed values")
        End Try
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        WriteData()
    End Sub
    Private Sub WriteData()
        Dim ID As Integer = TextBox6.Text
        Dim Dat As String = TextBox2.Text
        Dim Type As String = ComboBox1.SelectedItem
        Dim Person As String = TextBox3.Text
        Dim Add As String = TextBox4.Text
        Dim Remove As String = TextBox5.Text
        Dim Balance As String = TextBox9.Text

        If CheckBox3.Checked Then
            ID = ID + 1
            TextBox6.Text = ID
        End If

        If CheckBox1.Checked Then
            Dim Transactionxml As XElement = _
            <Transaction>
                <Dat id=<%= ID %>><%= Dat %></Dat>
                <Type id=<%= ID %>><%= Type %></Type>
                <Person id=<%= ID %>><%= Person %></Person>
                <Add id=<%= ID %>><%= Add %></Add>
                <Remove id=<%= ID %>><%= Remove %></Remove>
                <Balance id=<%= ID %>><%= Balance %></Balance>
            </Transaction>

            Form1.RichTextBox1.Text = Form1.RichTextBox1.Text & Transactionxml.ToString & vbCr

        End If

        If CheckBox2.Checked Then

            Dim Transactionxaml As XElement = _
            <List FontFamily="Arial" FontSize="11pt">
                <ListItem><Paragraph>ID:<%= ID %></Paragraph></ListItem>
                <ListItem><Paragraph>Date of Transaction:<%= Dat %></Paragraph></ListItem>
                <ListItem><Paragraph>Type of Transaction:<%= Type %></Paragraph></ListItem>
                <ListItem><Paragraph>By whom:<%= Person %></Paragraph></ListItem>
                <ListItem><Paragraph>Money Added:<%= Add %></Paragraph></ListItem>
                <ListItem><Paragraph>Money Removed:<%= Remove %></Paragraph></ListItem>
                <ListItem><Paragraph>Balance:<%= Balance %></Paragraph></ListItem>
             </List>

            Form1.RichTextBox2.Text = Form1.RichTextBox2.Text & Transactionxaml.ToString & vbCr
        End If
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        LoadData()
    End Sub
    Private Sub LoadData()
        Try
            Me.Text = "Accounting Database" & " - " & Form1.TextBox3.Text

            Dim ID As String = TextBox6.Text

            Dim Account As XElement = XElement.Load(Form1.TextBox7.Text & Form1.TextBox3.Text & ".xml")

            Dim Dat As XElement = _
                <Root>
                    <%= From el In Account.<Transaction>.<Dat> _
                        Where el.Attribute("id") = ID _
                        Select el %>
                </Root>
            TextBox2.Text = Dat

            Dim Type As XElement = _
                <Root>
                    <%= From el In Account.<Transaction>.<Type> _
                        Where el.Attribute("id") = ID _
                        Select el %>
                </Root>
            Dim Typet As String = Type
            ComboBox1.SelectedItem = Typet

            Dim Person As XElement = _
        <Root>
            <%= From el In Account.<Transaction>.<Person> _
                Where el.Attribute("id") = ID _
                Select el %>
        </Root>
            TextBox3.Text = Person

            Dim Add As XElement = _
        <Root>
            <%= From el In Account.<Transaction>.<Add> _
                Where el.Attribute("id") = ID _
                Select el %>
        </Root>
            TextBox4.Text = Add

            Dim Remove As XElement = _
        <Root>
            <%= From el In Account.<Transaction>.<Remove> _
                Where el.Attribute("id") = ID _
                Select el %>
        </Root>
            TextBox5.Text = Remove

            Dim Balance As XElement = _
               <Root>
                   <%= From el In Account.<Transaction>.<Balance> _
                       Where el.Attribute("id") = ID _
                       Select el %>
               </Root>
            TextBox9.Text = Balance

            Dim Owner As XElement = _
               <Root>
                   <%= From el In Account.<Owner> _
                       Select el %>
               </Root>
            TextBox1.Text = Owner


            Dim Count As XElement = _
        <Root>
            <%= From el In Account.<Counter>.<Count> _
                Select el %>
        </Root>

            Dim Counter As String = Count

            Label7.Text = "There are " & Counter & " record(s) in the XML file"
        Catch ex As Exception
            MsgBox("No file has been loaded.")

        End Try

    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        Try
            Do Until TextBox6.Text = TextBox8.Text
                CheckBox3.CheckState = CheckState.Unchecked
                Dim ID As Integer = TextBox6.Text
                ID = ID + 1
                TextBox6.Text = ID
                LoadData()
                WriteData()
            Loop
        Catch ex As Exception
            MsgBox("Choose a proper range.")
        End Try
    End Sub

    Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
        Nav.GoToFirst(TextBox6)

        LoadData()
    End Sub

    Private Sub ToolStripButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton2.Click
        Nav.PrevRec(TextBox6)

        LoadData()
    End Sub

    Private Sub ToolStripButton3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton3.Click
        Nav.NextRec(TextBox6)

        LoadData()
    End Sub

    Private Sub ToolStripButton4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton4.Click
        Nav.GoToLast(TextBox6, Form1.TextBox7.Text & Form1.TextBox3.Text & ".xml")

        LoadData()
    End Sub

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        Try
            Form13.Show()

            CheckBox3.CheckState = CheckState.Unchecked
            TextBox6.Text = 0

            LoadData()

            Dim ID As Integer = TextBox6.Text
            Dim XmlFile As XElement = XElement.Load(Form1.TextBox7.Text & Form1.TextBox3.Text & ".xml")

            Dim Counter As XElement = _
            <Root>
                <%= From el In XmlFile.<Counter>.<Count> _
                    Select el %>
            </Root>
            Dim Count As Integer = Counter

            Dim C As String = TextBox7.Text

            Dim Owner As String = TextBox1.Text

            Dim Ownerxml As XElement = _
            <Owner><%= Owner %></Owner>

            Form13.RichTextBox1.Text = Form13.RichTextBox1.Text & "<Account>" & vbCr _
                                      & "<Type>Accounting</Type>" & vbCr & Ownerxml.ToString & vbCr

            Do Until ID = Count
                ID = ID + 1
                TextBox6.Text = ID

                LoadData()

                Dim Dat As String = TextBox2.Text
                Dim Type As String = ComboBox1.SelectedItem
                Dim Person As String = TextBox3.Text
                Dim Add As String = TextBox4.Text
                Dim Remove As String = TextBox5.Text
                Dim Balance As String = TextBox9.Text

                Dim Transactionxml As XElement = _
                  <Transaction id=<%= ID %>>
                      <Dat><%= Dat %></Dat>
                      <Type><%= Type %></Type>
                      <Person><%= Person %></Person>
                      <Add><%= Add %></Add>
                      <Remove><%= Remove %></Remove>
                      <Balance><%= Balance %></Balance>
                  </Transaction>

                Form13.RichTextBox1.Text = Form13.RichTextBox1.Text & Transactionxml.ToString & vbCr

            Loop

            Dim LastRec As String = TextBox6.Text

            If CheckBox1.Checked Then
                Dim Counter2 As XElement = _
                <Counter>
                    <Count><%= LastRec %></Count>
                </Counter>

                Form13.RichTextBox1.Text = Form13.RichTextBox1.Text & Counter2.ToString & vbCr & "</Account>"
            End If

        Catch ex As Exception
            MsgBox("Processing Failed!")
        End Try
    End Sub
End Class